package com.cy.store.service.impl;

import com.cy.store.entity.Address;
import com.cy.store.mapper.AddressMapper;
import com.cy.store.service.AddressService;
import com.cy.store.service.DistrictService;
import com.cy.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private DistrictService districtService;
    @Value("${user.address.max-count}")
    private int maxCount;
    @Override
    public void addNewAddress(Integer uid, String username, Address address) {
        Integer count = addressMapper.countByUid(uid);
        if(count >= maxCount){
            throw new AddressCountLimitException("地址数量超出限制");
        }

        String provinceName = districtService.getNameByCode(address.getProvinceCode());
        String cityName = districtService.getNameByCode(address.getCityCode());
        String areaName = districtService.getNameByCode(address.getAreaCode());
        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);

        Integer isDefault = count==0? 1 : 0;
        address.setUid(uid);
        address.setIsDefault(isDefault);
        address.setCreatedTime(new Date());
        address.setCreatedUser(username);
        address.setModifiedTime(new Date());
        address.setModifiedUser(username);
        int row = addressMapper.insert(address);
        if(row!=1){
            throw new InsertException("地址插入异常");
        }
        System.out.println(row);
    }

    @Override
    public List<Address> getByUid(int uid) {
        List<Address> list = addressMapper.findByUid(uid);
        for(Address address : list){
//            address.setAid(null);
//            address.setUid(null);
            address.setProvinceCode(null);
            address.setCityCode(null);
            address.setAreaCode(null);
            address.setTel(null);
//            address.setIsDefault(null);
            address.setCreatedTime(null);
            address.setCreatedUser(null);
            address.setModifiedUser(null);
            address.setModifiedTime(null);
        }
        return list;
    }

    @Override
    public void setDefault(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        // 看收货地址是否存在
        if(result == null) throw new AddressNotFoundException("未找到收货地址");
        // 检测当前收货地址的归属
        if(!result.getUid().equals(uid)) throw new AccessDeniedException("非法访问");
        // 将所有收货地址设置为非默认
        Integer rows = addressMapper.updateNonDefault(uid);
        if(rows<1) throw new UpdateException("更新产生异常-无地址数据");
        rows = addressMapper.updateDefaultByAid(aid,username,new Date());
        if(rows!=1) throw new UpdateException("更新产生异常");
    }

    @Override
    public void delete(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if(result==null) throw new AddressNotFoundException("未找到地址信息");
        if(!result.getUid().equals(uid)) throw new AccessDeniedException("非法数据访问");
        if(result.getIsDefault()==1){
            // 看是否还有其他地址信息，设置最新修改的地址信息为1
            Integer count = addressMapper.countByUid(uid);
            if(count==0)  return;
            Address address = addressMapper.findLastModified(uid);
            Integer row = addressMapper.updateDefaultByAid(address.getAid(),username,new Date());
            if(row != 1) throw new UpdateException("更新默认地址时产生异常");
        }
        Integer rows = addressMapper.deleteByAid(aid);
        if(rows!=1) throw new DeleteException("删除地址信息产生未知异常");


    }

    @Override
    public Address getByAid(Integer aid,Integer uid) {
        Address address = addressMapper.findByAid(aid);
        if(address==null) throw new AddressNotFoundException("未找到地址");
        if (!address.getUid().equals(uid)) throw new AccessDeniedException("没有访问权限");
        address.setProvinceCode(null);
        address.setCityCode(null);
        address.setAreaCode(null);
        address.setModifiedUser(null);
        address.setCreatedUser(null);
        address.setModifiedTime(null);
        address.setCreatedTime(null);
        return address;
    }
}
